var app = getApp()
// var courseTime = null;
// var cChooseId = null;
Page({
  data: {
    hidden: true,
    date: null,
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    startYear: 2000,
    endYear: 2050,
    winWidth: 0,
    winHeight: 0,
    cChooseId: null,
    courseTime: null,
    sId: null,
    oneDay: null,//具体的某一天
    hasSign: null,//已经签到学生的数组
    notSign: null,//未签学生的数组
    tTodayCourse: null,
    list: [false, false, false, false, false, false],

    list1: [true, false, false, false, false, false, false, false, false, false],
    // tab切换  
    currentTab: 0,

    //分页相关
    pageSizehas: 10,
    pageNohas: 1,
    sum_pagehas: 1,
    sum_pagelisthas: null,


    pageSizenot: 10,
    pageNonot: 1,
    sum_pagenot: 1,
    sum_pagelistnot: null,


    sum_student: 0,
    sign_rate: " "
  },

  onLoad: function () {
    var that = this;
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var that = this
    var pages = getCurrentPages(); //获取当前页面信息栈
    var prevPage = pages[pages.length - 2] //获取上一个页面信息栈
    var _course = prevPage.data.cChooseId
    var _time = prevPage.data.cTime
    that.setData({
      sId: app.globalData.sId,
      courseTime: _time,
      cChooseId: _course
    })
    that.querySignInfor()
    app.globalData.socket.on("studentSign",
      (d) => {
        if (d != null) {
          that.querySignInfor()
        }
      });

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

  },

  onShow(e) {
    console.log("3meeyouma" + this.data.hasSign);
    var that = this
    this.setData({
      sId: app.globalData.sId
    })
    console.log("啥玩意");
    if (this.data.sId != null) {
      console.log("啥玩意2");
      wx.request({
        url: `https://geek-team.xin/schedule/wx_getTeacherScheduleByDay?tId=${app.globalData.sId}`,
        method: 'POST',
        success(res) {
          console.log(res)
          //{{item.cAddress}}{{item.cName}}{{item.tName}}{{item.time}}
          if (res.data != []) {
            that.setData({
              tTodayCourse: res.data
            })
          }
        },
        fail: (res) => console.log("啥玩意三" + res)
      })
    }
  },


  //多节课程view的选中事件
  choseOrNot(e) {
    console.log(e)
    for (var i = 0; i < this.data.list.length; i++) {
      console.log("呵呵1");
      if (i == e.currentTarget.dataset.index) {
        this.data.list[i] = true;
      } else {
        this.data.list[i] = false;
      }
    }

    this.setData({
      list: this.data.list,
      cChooseId: e.currentTarget.dataset.id,
      courseTime: e.currentTarget.dataset.time
    });
  },

  loadingChange() {

    setTimeout(() => {
      this.setData({
        hidden: true
      })
    }, 1000)
  },

  //这是查询学生签到详情信息按钮的点击事件
  querySignInfor() {
    this.setData({
      hidden: false
    })
    this.loadingChange()
    var that = this;
    that.setData({
      pageNohas: 1,
      sum_pagehas: 1,
      pageNonot: 1,
      sum_pagenot: 1,
    })
    that.getPage();
    wx.request({
      url: `https://geek-team.xin/signIn/getSignInfo`,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        // course: this.data.cChooseId,
        // courseTime: this.data.courseTime
        course: this.data.cChooseId,
        courseTime: this.data.courseTime
      },
      success(res) {
        console.log(res)
        var list1 = new Array();
        var list2 = new Array();
        if (res.data != []) {
          console.log(res.data.studentCount)
          that.setData({
            sum_pagehas: Math.ceil((res.data.signCount) / 10),
            sum_pagenot: Math.ceil((res.data.studentCount - res.data.signCount) / 10),
            sum_student: res.data.studentCount,
            sign_rate: (res.data.signRate).toFixed(2)

          })

          for (var i = 0; i < that.data.sum_pagehas; i++) {
            list1[i] = i;

          }
          for (var i = 0; i < that.data.sum_pagenot; i++) {
            list2[i] = i;

          }
          that.setData({
            sum_pagelisthas: list1,
            sum_pagelistnot: list2
          })
        }
      },
      fail: (res) => console.log("错误" + res)
    })

  },


  //分页
  getPage() {
    var that = this;
    console.log(that.data.cChooseId + "::" + that.data.courseTime + "::" + that.data.pageNohas + "::" + that.data.pageSizenot + "::" + that.data.pageNonot),

      wx.request({
        url: `https://geek-team.xin/signIn/getSignInfoByPage`,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          course: that.data.cChooseId,
          courseTime: that.data.courseTime,
          pageSizeHas: that.data.pageSizehas,
          pageSizeNot: that.data.pageSizenot,
          pageNoHas: that.data.pageNohas,
          pageNoNot: that.data.pageNonot

        },
        success(res) {
          console.log(res)
          var list1 = new Array();
          var list2 = new Array();
          if (res.data != []) {
            var _hasSign = JSON.parse(res.data.hasSign);
            var _notSign = JSON.parse(res.data.notSign);
            console.log(_hasSign);
            console.log(_notSign);
            that.setData({
              hasSign: _hasSign,
              notSign: _notSign
            })

          } else {
            that.setData({
              hasSign: null,
              notSign: null,
              sum_pagelisthas: null,
              sum_pagelistnot: null
            })
          }
        },
        fail: (res) => console.log("错误" + res)
      })
  },
  //得到已签学生的当前页
  getcurrentpage(e) {
    var that = this;
    that.data.list1[i] = false;
    console.log(e);

    var pageno = e.currentTarget.dataset.pagenohas;
    var type = e.currentTarget.dataset.type;
    for (var i = 0; i < that.data.list1.length; i++) {
      console.log(i + "::" + pageno + "::" + that.data.list1);
      if (i == pageno - 1) {
        that.data.list1[i] = true;
      } else {
        that.data.list1[i] = false;
      }
    }
    that.setData({
      list1: this.data.list1,
    })
    console.log(pageno + "::" + type);
    if (type == "has") {
      that.setData({
        pageNohas: pageno
      })
      that.getPage();
    } else {
      that.setData({
        pageNonot: pageno
      })
      that.getPage();
    }
  },

  //上一页
  previous(e) {
    var that = this;
    console.log(e);
    var type = e.currentTarget.dataset.type;
    if (type == "has") {
      if (that.data.pageNohas > 1) {
        that.setData({
          pageNohas: that.data.pageNohas - 1
        })
        that.getPage();
      }
    } else {
      if (that.data.pageNonot > 1) {
        that.setData({
          pageNonot: that.data.pageNonot - 1
        })
        that.getPage();
      }

    }
  },

  //下一页
  next(e) {
    var that = this;
    console.log(e);
    var type = e.currentTarget.dataset.type;
    if (type == "has") {
      console.log("什么？？");
      console.log("什么？？" + that.data.pageNohas + "::" + that.data.sum_pagehas);
      if (that.data.pageNohas < that.data.sum_pagehas) {
        that.setData({
          pageNohas: that.data.pageNohas + 1
        })
        that.getPage();
      }
    } else {
      if (that.data.pageNonot < that.data.sum_pagenot) {
        that.setData({
          pageNonot: that.data.pageNonot + 1
        })
        that.getPage();
      }

    }


  },


  //改变日期后就联网获取当天课程




  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {

    var that = this;
    that.setData({


      currentTab: e.detail.current


    });

  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  }
}
)  